package com.fancy.learncenter.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.fancy.learncenter.activity.base.BaseActivity;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.YgtDataBean;
import com.fancy.learncenter.bean.YgtPayDataBean;
import com.fancy.learncenter.common.MyApplication;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.ToastUtil;
import com.google.gson.Gson;
import com.superservice.lya.R;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import fancy.hyypaysdk.pay.PayCallBack;
import fancy.hyypaysdk.pay.wxpay.WXPayUtil;

public class YgtPayActivity extends BaseActivity {

    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.wx_icon)
    ImageView wxIcon;
    @Bind(R.id.person_icon)
    ImageView personIcon;
    @Bind(R.id.card_icon)
    ImageView cardIcon;
    @Bind(R.id.payNum)
    TextView payNum;
    @Bind(R.id.card_pws)
    EditText cardPws;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ygt_payi, getResources().getColor(R.color.toolbar_color));
        ButterKnife.bind(this);
        initToolbar("易沟通充值");
    }

    @OnClick({R.id.wx_ll, R.id.card_ll, R.id.person_ll, R.id.pay})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.wx_ll:
                changeIcon(2);
                break;
            case R.id.card_ll:
                changeIcon(3);
                break;
            case R.id.person_ll:
                changeIcon(1);
                break;
            case R.id.pay:
//                changeIcon(3);
//                submit();

                if (index == 3) {
                    if (TextUtils.isEmpty(cardPws.getText())) {
                        ToastUtil.show("请输入卡的密码");
                        return;
                    } else {
                        submit();
                    }
                } else {
                    submit();
//                    wxPay();
                }

                break;
        }
    }

    //    类型:1余额,2微信,3充值卡
    private int index = 2;

    private void changeIcon(int index) {
        this.index = index;
        wxIcon.setImageResource(R.drawable.xrefresh_ok);
        personIcon.setImageResource(R.drawable.xrefresh_ok);
        cardIcon.setImageResource(R.drawable.xrefresh_ok);

        if (index == 1) {
            personIcon.setImageResource(R.mipmap.bg_gouxuan);
        } else if (index == 2) {
            wxIcon.setImageResource(R.mipmap.bg_gouxuan);
        } else if (index == 3) {
            cardIcon.setImageResource(R.mipmap.bg_gouxuan);
        }
    }


    private void submit() {

        Map maps = new HashMap();
        maps.put("type", index + "");
        maps.put("price", "298");
        maps.put("month", "12");
        maps.put("card", cardPws.getText().toString());

        HttpMehtod.getInstance().payYgt(maps, new IdeaObserver<BaseDataBean<YgtPayDataBean>>(this, true) {


            @Override
            public void onSuccess(BaseDataBean<YgtPayDataBean> ygtPayDataBeanBaseDataBean) {

                if (index == 2) {

                    if (ygtPayDataBeanBaseDataBean.getData() != null) {
                        Gson gson = new Gson();
                        String json = gson.toJson(ygtPayDataBeanBaseDataBean.getData());

                        WXPayUtil.getInstance().wxPay(YgtPayActivity.this, json, new PayCallBack() {
                            @Override
                            public void paySuccess() {
                                ToastUtil.show("支付成功");
                                finish();
                            }

                            @Override
                            public void payFaild() {
                                ToastUtil.show("支付失败");
                            }

                            @Override
                            public void payCancle() {
                                ToastUtil.show("支付取消");
                            }
                        });
                    }

                } else {
                    ToastUtil.show("充值成功");
                    MyApplication.is_device = "1";
                    finish();
                }
            }

            @Override
            public void onFail(int errorCode) {

            }

        });

    }

    private void wxPay() {

        Map maps = new HashMap();
        maps.put("payMoney", "98");
        HttpMehtod.getInstance().wxPay(maps, new IdeaObserver<BaseDataBean>() {

            @Override
            public void onSuccess(BaseDataBean stringBaseDataBean) {
                ToastUtil.show("充值成功");
                MyApplication.is_device = "1";
                finish();
            }

            @Override
            public void onFail(int errorCode) {

            }

        });
    }
}
